/**
 * Created by Administrator on 2015/6/22 0022.
 */
define('side', ['avalon'], function () {
    return side = avalon.define({
        $id: "side",
        ready: function () {
            side.Event()
            console.log("side加载成功")
            avalon.scan()
        },

        playerListM: [],
        playerListW: [],
        Event: function () {
            //绑定用户进入退出事件
            ws.addListen('event', function (v) {
                if (v[0] == "player") {
                    //用户相关事件
                    switch (v[1]) {
                        case "PlayerList":
                            //获得用户列表
                            var str = v[2].replace(/\[\|\]/g, ",")
                            var data = JSON.parse(str)
                            console.log(data)
                            var wl = [],
                                ml = data.M;

                            var x;
                            for (x in data.W) {
                                wl.push(data.W[x])
                            }

                            side.playerListM = ml;
                            side.playerListW = wl;

                            break;
                        case 'PlayerJoinEvent':
                            var t = v[3];
                            if (t == "M\r\n") {
                                side.playerListM.push(v[2])
                            }
                            else if (t == "W\r\n") {
                                side.playerListW.push(v[2])
                            }


                            //用户加入
                            break;
                        case 'PlayerQuitEvent':
                            //用户离线
                            var c = v[3];
                            if (c == "M\r\n") {
                                delP("playerListM")
                            }
                            else if (c == "W\r\n") {
                                delP("playerListW")
                            }
                            function delP(listName){
                                for (var i = 0; i < side[listName].length; i++) {
                                    if (side[listName][i] == v[2]) {
                                        side[listName] = side[listName].splice(i, 1)
                                        break;
                                    }
                                }
                            }


                            break;
                    }

                }
            })
        }



    })
})